﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System;
using WebApi.Model;
using WebApi.ViewModel;
using WebApi.Helpers;

namespace WebApi.Controllers
{
    public class LogController : BaseController
    {
        public LogController(ILogger<LogController> logger) : base(logger)
        {
        }

        /// <summary>
        /// 获取数据列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("GetList")]
        public async Task<ReturnModel> GetList(Log model)
        {
            try
            {
                Expression<Func<Log, bool>> filter = e => true;
                if (!string.IsNullOrEmpty(model.OpeType))
                    filter = filter.And(c => c.OpeType.Contains(model.OpeType));
                if (!string.IsNullOrEmpty(model.OpeDetail))
                    filter = filter.And(c => c.OpeDetail.Contains(model.OpeDetail));
                if (model.BeginTime != null)
                    filter = filter.And(c => c.AddTime > model.BeginTime);
                if (model.EndTime != null)
                    filter = filter.And(c => c.AddTime < model.EndTime.Value.AddDays(1));
                SqlSugar.PageModel pageModel = new SqlSugar.PageModel { PageIndex = model.PageIndex, PageSize = model.PageSize };
                var data = await DbHelper.Log.GetPageListAsync(filter, pageModel);
                return new ReturnModel { Count = pageModel.TotalCount, Data = data };
            }
            catch (Exception ex)
            {
                return logger.ErrorModel($"{nameof(LogController)}/{nameof(GetList)}", ex);
            }
        }
    }
}
